package io.gitee.xyuesheng.day08.Thread;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

//线程池
public class TestThreadPool {
    public static void main(String[] args) {
        //Executors执行器
        ExecutorService service = Executors.newFixedThreadPool(4);

        //给线程池执行器提交一个任务，这个任务会跑在一个单独的线程里面
//        service.submit(new Runnable() {
//            @Override
//            public void run() {
//                System.out.println("异步任务。。");
//            }
//        });

        //创建10个线程
        for (int i = 0; i < 10; i++) {
            service.submit(() -> {
                try {
                    Thread.sleep(500);

                    //打印当前线程的名字
                    System.out.println("线程。。" + Thread.currentThread());

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
        }

        service.shutdown();//关闭执行器

    }
}
